<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>历史记录</title>
    <!-- 引入Google字体 -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap" rel="stylesheet">
    <!-- 引入Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- 引入Font Awesome图标库 -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
    <!-- 自定义CSS -->
    <style>
        :root {
            --background-color: #667eea;
            --background-end-color: #764ba2;
            --text-color: #fff;
            --sidebar-background: rgba(255, 255, 255, 0.1);
            --sidebar-backdrop-filter: blur(10px);
            --sidebar-box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
            --container-background: rgba(255, 255, 255, 0.1);
            --container-border-radius: 20px;
            --container-backdrop-filter: blur(10px);
            --container-box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            --history-item-background: rgba(255, 255, 255, 0.1);
            --history-item-hover-background: rgba(255, 255, 255, 0.15);
            --btn-primary-background: rgba(255, 255, 255, 0.1);
            --btn-danger-background: rgba(255, 99, 71, 0.8);
            --pagination-background: rgba(255, 255, 255, 0.1);
            --pagination-hover-background: rgba(255, 255, 255, 0.2);
            --search-input-background: rgba(255, 255, 255, 0.1);
            --search-input-focus-background: rgba(255, 255, 255, 0.2);
            --filter-input-background: rgba(255, 255, 255, 0.1);
            --filter-input-focus-background: rgba(255, 255, 255, 0.2);
        }

        [data-theme="dark"] {
            --background-color: #121212;
            --background-end-color: #1e1e1e;
            --text-color: #fff;
            --sidebar-background: rgba(0, 0, 0, 0.1);
            --sidebar-backdrop-filter: blur(10px);
            --sidebar-box-shadow: 2px 0 10px rgba(0, 0, 0, 0.1);
            --container-background: rgba(0, 0, 0, 0.1);
            --container-border-radius: 20px;
            --container-backdrop-filter: blur(10px);
            --container-box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            --history-item-background: rgba(0, 0, 0, 0.1);
            --history-item-hover-background: rgba(0, 0, 0, 0.15);
            --btn-primary-background: rgba(0, 0, 0, 0.1);
            --btn-danger-background: rgba(255, 99, 71, 0.8);
            --pagination-background: rgba(0, 0, 0, 0.1);
            --pagination-hover-background: rgba(0, 0, 0, 0.2);
            --search-input-background: rgba(0, 0, 0, 0.1);
            --search-input-focus-background: rgba(0, 0, 0, 0.2);
            --filter-input-background: rgba(0, 0, 0, 0.1);
            --filter-input-focus-background: rgba(0, 0, 0, 0.2);
        }

        /* 全局样式 */
        body {
            font-family: 'Poppins', sans-serif;
            background: linear-gradient(135deg, var(--background-color), var(--background-end-color));
            color: var(--text-color);
            min-height: 100vh;
            margin: 0;
            padding: 0;
            display: flex;
        }

        /* 左侧菜单样式 */
        .sidebar {
            width: 250px;
            background: var(--sidebar-background);
            backdrop-filter: var(--sidebar-backdrop-filter);
            padding: 20px;
            box-shadow: var(--sidebar-box-shadow);
        }

        .sidebar h3 {
            font-size: 1.5rem;
            font-weight: 600;
            margin-bottom: 20px;
            color: var(--text-color);
            text-align: center;
        }

        .sidebar ul {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        .sidebar ul li {
            margin-bottom: 10px;
        }

        .sidebar ul li a {
            display: block;
            padding: 10px;
            background: rgba(255, 255, 255, 0.1);
            border-radius: 10px;
            color: var(--text-color);
            text-decoration: none;
            transition: background 0.3s ease;
        }

        .sidebar ul li a:hover {
            background: rgba(255, 255, 255, 0.2);
        }

        /* 右侧内容样式 */
        .content {
            flex: 1;
            padding: 40px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 40px;
            background: var(--container-background);
            border-radius: var(--container-border-radius);
            backdrop-filter: var(--container-backdrop-filter);
            box-shadow: var(--container-box-shadow);
        }

        h1 {
            font-size: 2.5rem;
            font-weight: 600;
            margin-bottom: 30px;
            text-align: center;
            color: var(--text-color);
        }

        .history-item {
            background: var(--history-item-background);
            padding: 20px;
            border-radius: 15px;
            margin-bottom: 20px;
            transition: transform 0.3s ease, box-shadow 0.3s ease, background-color 0.3s ease;
            animation: fadeInUp 0.5s ease-out;
            animation-fill-mode: both;
        }

        .history-item:hover {
            transform: translateY(-10px) scale(1.02);
            box-shadow: 0 16px 32px rgba(0, 0, 0, 0.2);
            background: var(--history-item-hover-background); /* 稍微加深背景色 */
        }

        .history-item img {
            max-width: 100%;
            border-radius: 10px;
            margin-bottom: 15px;
            transition: opacity 0.3s ease;
        }

        .history-item img:hover {
            opacity: 0.9;
        }

        .history-item p {
            margin: 5px 0;
            font-size: 1rem;
            color: var(--text-color);
        }

        .btn-primary,
        .btn-danger {
            background: var(--btn-primary-background);
            border: none;
            border-radius: 25px;
            padding: 10px 20px;
            color: var(--text-color);
            transition: background 0.3s ease;
            position: relative;
            overflow: hidden;
            cursor: pointer;
        }

        .btn-primary:hover,
        .btn-danger:hover {
            background: var(--pagination-hover-background);
        }

        .btn-primary::before,
        .btn-danger::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 200%;
            height: 100%;
            background: rgba(255, 255, 255, 0.3);
            transform: skewX(-20deg);
            transition: left 0.3s ease;
        }

        .btn-primary:hover::before,
        .btn-danger:hover::before {
            left: 0;
        }

        .btn-primary {
            margin-top: 40px;
        }

        .btn-danger {
            margin-top: 20px;
        }

        /* 动画效果 */
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .history-item:nth-child(odd) {
            animation-delay: 0.2s;
        }

        .history-item:nth-child(even) {
            animation-delay: 0.4s;
        }

        /* 分页样式 */
        .pagination {
            display: flex;
            justify-content: center;
            margin-top: 30px;
        }

        .pagination a {
            margin: 0 5px;
            padding: 8px 16px;
            background: var(--pagination-background);
            border-radius: 20px;
            color: var(--text-color);
            text-decoration: none;
            transition: background 0.3s ease;
        }

        .pagination a:hover {
            background: var(--pagination-hover-background);
        }

        .pagination span {
            margin: 0 10px;
            font-size: 1rem;
            color: rgba(255, 255, 255, 0.8);
        }

        /* 搜索框和筛选样式 */
        .search-container {
            margin-bottom: 20px;
        }

        .search-container input[type="text"] {
            width: 100%;
            padding: 10px;
            border: none;
            border-radius: 25px;
            background: var(--search-input-background);
            color: var(--text-color);
            transition: background 0.3s ease;
        }

        .search-container input[type="text"]:focus {
            background: var(--search-input-focus-background);
            outline: none;
        }

        .filter-container {
            display: flex;
            justify-content: space-between;
            margin-bottom: 20px;
        }

        .filter-container input[type="date"] {
            padding: 10px;
            border: none;
            border-radius: 25px;
            background: var(--filter-input-background);
            color: var(--text-color);
            transition: background 0.3s ease;
        }

        .filter-container input[type="date"]:focus {
            background: var(--filter-input-focus-background);
            outline: none;
        }

        /* 按钮组样式 */
        .btn-group {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }

        .btn-group form {
            display: inline-block;
        }

        .btn-group .btn-primary {
            margin-left: 10px;
        }

        /* 图片放大效果 */
        .zoomable-image {
            position: relative;
            display: inline-block;
        }

        .zoomable-image::after {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-position: center;
            background-repeat: no-repeat;
            background-size: 200% 200%; /* 放大倍数 */
            opacity: 0;
            transition: opacity 0.3s ease;
            pointer-events: none;
            border-radius: 10px;
        }

        .zoomable-image:hover::after {
            opacity: 1;
        }

        .zoomable-image img {
            width: 100%;
            height: auto;
            border-radius: 10px;
            transition: transform 0.3s ease;
        }

        .zoomable-image:hover img {
            transform: scale(1.1); /* 缩小图片以适应放大镜 */
        }

        /* 切换按钮样式 */
        .theme-toggle {
            position: fixed;
            top: 20px;
            right: 20px;
            z-index: 1000;
            background: rgba(255, 255, 255, 0.1);
            border: none;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: background 0.3s ease;
        }

        .theme-toggle:hover {
            background: rgba(255, 255, 255, 0.2);
        }

        .theme-toggle i {
            color: var(--text-color);
            font-size: 1.5rem;
        }
    </style>
</head>
<body>
    <!-- 主题切换按钮 -->
    <button class="theme-toggle" id="themeToggleBtn">
        <i class="fas fa-adjust"></i>
    </button>

    <!-- 左侧菜单 -->
    <div class="sidebar">
        <h3>分类类别</h3>
        <ul>
            <li><a href="/history?category=飞机"><i class="fas fa-plane"></i> 飞机</a></li>
            <li><a href="/history?category=汽车"><i class="fas fa-car"></i> 汽车</a></li>
            <li><a href="/history?category=鸟"><i class="fas fa-dove"></i> 鸟</a></li>
            <li><a href="/history?category=猫"><i class="fas fa-cat"></i> 猫</a></li>
            <li><a href="/history?category=鹿"><i class="fas fa-deer"></i> 鹿</a></li>
            <li><a href="/history?category=狗"><i class="fas fa-dog"></i> 狗</a></li>
            <li><a href="/history?category=青蛙"><i class="fas fa-frog"></i> 青蛙</a></li>
            <li><a href="/history?category=马"><i class="fas fa-horse"></i> 马</a></li>
            <li><a href="/history?category=船"><i class="fas fa-ship"></i> 船</a></li>
            <li><a href="/history?category=卡车"><i class="fas fa-truck"></i> 卡车</a></li>
        </ul>
    </div>

    <!-- 右侧内容 -->
    <div class="content">
        <div class="container">
            <!-- 搜索框 -->
            <div class="search-container">
                <input type="text" id="searchInput" placeholder="搜索历史记录...">
            </div>

            <!-- 筛选容器 -->
            <div class="filter-container">
                <input type="date" id="dateFilter">
            </div>

            <!-- 按钮组 -->
            <div class="btn-group">
                <form action="/clear_history" method="POST" style="display: inline;">
                    <button type="submit" class="btn btn-danger">清除历史记录</button>
                </form>
                <div class="text-center">
                    <a href="/" class="btn btn-primary">返回主页</a>
                </div>
            </div>

            <h1>历史记录 - {{ category if category else '全部' }}</h1>

            <!-- 历史记录列表 -->
            <div id="history-list">
                {% if predictions.items|length == 0 %}
                    <div class="text-center">
                        <p>暂无历史记录。</p>
                    </div>
                {% else %}
                    {% for prediction in predictions.items %}
                    <div class="history-item" data-date="{{ prediction.timestamp.strftime('%Y-%m-%d') }}">
                        <div class="zoomable-image">
                            <img src="{{ prediction.image_url }}" alt="历史图片" class="preview" loading="lazy">
                        </div>
                        <p><strong>类别：</strong>{{ prediction.predicted_class }}</p>
                        <p><strong>置信度：</strong>{{ prediction.confidence }}%</p>
                        <p><strong>时间：</strong>{{ prediction.timestamp.strftime('%Y-%m-%d %H:%M:%S') }}</p>
                        <form action="/delete/{{ prediction.id }}" method="POST" style="display: inline;" class="single-delete-form">
                            <button type="submit" class="btn btn-sm btn-danger">删除</button>
                        </form>
                    </div>
                    {% endfor %}
                {% endif %}
            </div>

            <!-- 分页控件 -->
            <div class="pagination">
                {% if predictions.has_prev %}
                    <a href="{{ url_for('history', category=category, page=predictions.prev_num) }}">上一页</a>
                {% endif %}
                <span>第 {{ predictions.page }} 页 / 共 {{ predictions.pages }} 页</span>
                {% if predictions.has_next %}
                    <a href="{{ url_for('history', category=category, page=predictions.next_num) }}">下一页</a>
                {% endif %}
            </div>
        </div>
    </div>

    <!-- 引入Bootstrap 5 JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 获取元素
            const searchInput = document.getElementById('searchInput');
            const dateFilter = document.getElementById('dateFilter');
            const historyItems = document.querySelectorAll('.history-item');
            const themeToggleBtn = document.getElementById('themeToggleBtn');

            // 初始化主题
            const userThemePreference = localStorage.getItem('theme');
            if (userThemePreference === 'dark') {
                document.documentElement.setAttribute('data-theme', 'dark');
            } else if (userThemePreference === 'light') {
                document.documentElement.removeAttribute('data-theme');
            } else {
                // 默认使用系统的主题设置
                if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
                    document.documentElement.setAttribute('data-theme', 'dark');
                }
            }

            // 主题切换按钮点击事件
            themeToggleBtn.addEventListener('click', () => {
                const isDarkMode = document.documentElement.getAttribute('data-theme') === 'dark';
                if (isDarkMode) {
                    document.documentElement.removeAttribute('data-theme');
                    localStorage.setItem('theme', 'light');
                } else {
                    document.documentElement.setAttribute('data-theme', 'dark');
                    localStorage.setItem('theme', 'dark');
                }
            });

            // 搜索功能
            searchInput.addEventListener('keyup', function() {
                const query = searchInput.value.toLowerCase();
                historyItems.forEach(item => {
                    const textContent = item.textContent.toLowerCase();
                    if (textContent.includes(query)) {
                        item.style.display = '';
                    } else {
                        item.style.display = 'none';
                    }
                });
            });

            // 日期筛选功能
            dateFilter.addEventListener('change', function() {
                const selectedDate = dateFilter.value;
                historyItems.forEach(item => {
                    const date = item.getAttribute('data-date');
                    if (selectedDate === '' || date === selectedDate) {
                        item.style.display = '';
                    } else {
                        item.style.display = 'none';
                    }
                });
            });

            // Prevent form submission on single delete button click
            const singleDeleteForms = document.querySelectorAll('.single-delete-form');
            singleDeleteForms.forEach(form => {
                form.addEventListener('submit', function(event) {
                    event.preventDefault();
                    if (confirm('确定要删除这条记录吗？')) {
                        this.submit();
                    }
                });
            });

            // 添加鼠标移动事件监听器以调整放大镜位置
            const zoomableImages = document.querySelectorAll('.zoomable-image');
            zoomableImages.forEach(zoomableImage => {
                zoomableImage.addEventListener('mousemove', function(event) {
                    const rect = zoomableImage.getBoundingClientRect();
                    const x = event.clientX - rect.left;
                    const y = event.clientY - rect.top;

                    const offsetX = ((x / rect.width) * 100) - 50;
                    const offsetY = ((y / rect.height) * 100) - 50;

                    zoomableImage.style.backgroundPosition = `${offsetX}% ${offsetY}%`;
                });
            });
        });
    </script>
</body>
</html>


